package com.zany.util.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.frame.action.HeaderAction;
import com.zany.frame.action.MenuAction;
import com.zany.login.action.LoginAction;
import com.zany.util.ajax.AjaxTools;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * session拦截器
 * @author chengY
 * @version 1.0
 */
public class SessionInterceptor  extends AbstractInterceptor {
	
	private static final long serialVersionUID = -4514700425300485631L;
	private static Log logger = LogFactory.getLog(SessionInterceptor.class);
	
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		HttpServletResponse response ;
		response = ServletActionContext.getResponse();
		Map session = actionInvocation.getInvocationContext().getSession();
		Action action = (Action) actionInvocation.getAction();
		if(action instanceof MenuAction)
			if(logger.isInfoEnabled()){logger.info("action instanceof MenuAction");}
		if(action instanceof LoginAction){
			return actionInvocation.invoke();
		}else{
			if(session.get("userInfo")== null){
//				if(action instanceof HeaderAction){
//				((ActionSupport) action).addActionError("登陆超时，请重新登陆！");
//				return "header";
//				}
//				return action.LOGIN;
//				AjaxTools.exAjax("登陆超时",response);
				//((ActionSupport) action).addActionError("登陆超时，请重新登陆！");
//				AjaxTools.exAjax("{\"success\":false,msg:\"登陆超时，请重新登陆！\"}",response);
				return "error";
			}
			else
				return actionInvocation.invoke();
		}
	}

}
